﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace z {
	public partial class Main : Form {
		Excel.Application excelApp;
		public Main() {
			InitializeComponent();
			excelApp = new Excel.Application();
			excelApp.Workbooks.Add(@"C:\Users\Public\74Dat\HoaDonBanLe.xls");
			excelApp.Visible = true;
		}

		private void btnCal_Click(object sender, EventArgs e) {
			int row = 10;
			double sum = 0,sumVat=0;
			int i = 1;
			excelApp.Cells[7, "B"].Value = "Ngày: " + DateTime.Today.ToString("dd/MM/yyyy");
			bool isVat = excelApp.Cells[row, "E"].Value == null;
			while (true) {
				if (excelApp.Cells[row, "D"].Value == null || (!isVat && excelApp.Cells[row, "E"].Value == null)
					|| (isVat && excelApp.Cells[row, "G"].Value == null) || row == 21)
					break;
				excelApp.Cells[row, "A"].Value = i++;
				if (isVat) {
					sumVat += excelApp.Cells[row, "G"].Value * 1000 * excelApp.Cells[row, "D"].Value;
					excelApp.Cells[row, "E"].Value = Math.Round(excelApp.Cells[row, "G"].Value / 1.1 * 1000);
				} else
					excelApp.Cells[row, "E"].Value = excelApp.Cells[row, "E"].Value * 1000;
				excelApp.Cells[row, "E"].NumberFormat = "#,##0";
				excelApp.Cells[row, "F"].Value = excelApp.Cells[row, "D"].Value * excelApp.Cells[row, "E"].Value;
				excelApp.Cells[row, "F"].NumberFormat = "#,##0";
				sum += excelApp.Cells[row, "F"].Value;
				row++;
			}
			if (isVat) {
				excelApp.Cells[21, "F"].Value = sum;
				excelApp.Cells[21, "F"].NumberFormat = "#,##0";
				excelApp.Cells[22, "F"].Value = sumVat-sum;
				excelApp.Cells[22, "F"].NumberFormat = "#,##0";
				excelApp.Cells[23, "F"].Value = sumVat;
				excelApp.Cells[23, "F"].NumberFormat = "#,##0";
				excelApp.Cells[24, "A"].Value = c.ZM.tienthanhchu(sumVat);
			} else {
				excelApp.Cells[23, "F"].Value = sum;
				excelApp.Cells[23, "F"].NumberFormat = "#,##0";
				excelApp.Cells[24, "A"].Value = c.ZM.tienthanhchu(sum);
				excelApp.Rows[21].Delete();
				excelApp.Rows[21].Delete();
			}
		}
	}
}
